package hwaniiiii.dodol;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

public class ScheduleAndResultActivity extends Activity {
	private WorldCupApp mApp;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApp = (WorldCupApp)getApplication();

        
        setContentView(R.layout.scheduleandresult);
        
        ListView lv = (ListView) findViewById(R.id.schedule_and_result_list);
        
        String search = getIntent().getStringExtra("search");
        if(search == null) {
        	lv.setAdapter(mApp.getListAdapter());	
        } else {
        	lv.setAdapter(mApp.getListAdapter(search));
        }
        
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		
		TextView tv = (TextView)findViewById(R.id.text_bar);
		tv.setText(mApp.getTextBarString());
		
		ScheduleAndResultAdapter adapter = (ScheduleAndResultAdapter) ((ListView) findViewById(R.id.schedule_and_result_list)).getAdapter();
		
		int monthToday = Integer.valueOf((String) DateFormat.format("MM", System.currentTimeMillis()));
        int dayToday = Integer.valueOf((String) DateFormat.format("dd", System.currentTimeMillis()));
        
		
		for(int i = 0; i < adapter.getCount(); i++) {
			MatchData md = (MatchData) adapter.getItem(i);
			
//			Log.v("myra", "month " + md.getMonth() + " " + md.getDay());
			if(md.getMonth() == monthToday && md.getDay() == dayToday) {
				((ListView) findViewById(R.id.schedule_and_result_list)).setSelection((i-1 > 0 ) ? (i - 1): 0);
				return;
			}
		}
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	mApp.getAddMenu(menu);
        return true;
    }

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	mApp.getAboutDialog(this).show();
    	return true;
    }
	
}
